package com.example.vaccine.mapper;

import com.example.vaccine.entity.VaccineUsage;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author Coisn
 * @since 2025-06-12
 */
@Mapper
public interface VaccineUsageMapper extends BaseMapper<VaccineUsage> {
    @Select({
            "<script>",
            "SELECT COALESCE(SUM(quantity_used),0) FROM vaccine_usage",
            "<if test='startTime != null'> WHERE usage_time &gt;= #{startTime} </if>",
            "</script>"
    })
    Integer sumQuantityUsedAfter(@Param("startTime") LocalDateTime startTime);

    @Select({
            "<script>",
            "SELECT v.name as vaccineName, COALESCE(SUM(u.quantity_used),0) as totalUsed",
            "FROM vaccines v",
            "LEFT JOIN vaccine_usage u ON v.numbering = u.vaccine_numbering",
            "<if test='startTime != null'> AND u.usage_time &gt;= #{startTime} </if>",
            "GROUP BY v.name",
            "ORDER BY totalUsed DESC",
            "</script>"
    })
    List<Map<String, Object>> selectUsageRanking(@Param("startTime") LocalDateTime startTime);

    List<Map<String, Object>> selectUsageWithVaccineName(@Param("doctorId") Integer doctorId);
}
